package com.plant.address.controller;

import com.plant.address.domain.MyAddress;
import com.plant.address.service.MyAddressService;
import com.plant.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("address")
public class MyAddressController {

    @Autowired
    private MyAddressService myAddressService;

    @GetMapping("findAllAddress")
    public Result findAllAddress(MyAddress myAddress) {
      List<MyAddress> list = myAddressService.findAllAddress(myAddress);
      return Result.ok(list);
    }
    @GetMapping("findById/{userId}")
    public Result findById(@PathVariable Integer userId){
        List<MyAddress> myAddress = myAddressService.findById(userId);
        return Result.ok(myAddress);
    }

    @GetMapping("findByAddId/{id}")
    public Result findByAddId(@PathVariable Integer id){
        MyAddress myAddress = myAddressService.findByAddId(id);
        return Result.ok(myAddress);
    }
    @PostMapping("myAddressAdd")
    public Result add(@RequestBody MyAddress myAddress){
        System.out.println(myAddress);
        int a  = myAddressService.myAddressAdd(myAddress);
        if(a>0){
            return Result.ok(a);
        }else {
            return Result.fail(400,"添加失败");
        }
    }

    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id){
        int a = myAddressService.delete(id);
        if(a>0){
            return Result.ok(a);
        }else {
            return Result.fail(400,"删除失败");
        }
    }

    @PostMapping("update")
    public Result update(@RequestBody MyAddress myAddress){
        int a = myAddressService.update(myAddress);
        if(a>0){
            return Result.ok(a);
        }else {
            return Result.fail(400,"修改失败");
        }
    }
}
